package com.black.play.listener;

import com.black.play.component.SudokuWindow;
import com.black.play.constant.AppConstant;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @author ylx
 */
public class SudokuKeyListener extends KeyAdapter
{

    /**
     * 键盘按键监听
     *
     * @param e 按键事件
     */
    @Override
    public void keyPressed(KeyEvent e)
    {
        int keyCode = e.getKeyCode();
        switch (keyCode)
        {
            case KeyEvent.VK_ESCAPE:
                handleEscape();
                break;
            case KeyEvent.VK_SPACE:
                handleSpace();
                break;
        }
    }

    /**
     * 空格
     * 游戏→暂停
     * 暂停→游戏
     * 菜单→开始
     */
    private void handleSpace()
    {
        SudokuWindow sudokuWindow = SudokuWindow.getInstance();
        String activeCard = sudokuWindow.getActiveCard();
        switch (activeCard)
        {
            case AppConstant.GAME:
                sudokuWindow.pauseGame();
                break;
            case AppConstant.PAUSE:
                sudokuWindow.continueGame();
                break;
            case AppConstant.MENU:
                sudokuWindow.startGame();
                break;
        }
    }

    /**
     * Esc
     * 菜单→退出
     * 暂停→退出
     * 游戏→暂停
     */
    private void handleEscape()
    {
        SudokuWindow sudokuWindow = SudokuWindow.getInstance();
        String activeCard = sudokuWindow.getActiveCard();
        switch (activeCard)
        {
            case AppConstant.GAME:
                sudokuWindow.pauseGame();
                break;
            case AppConstant.PAUSE:
            case AppConstant.MENU:
                sudokuWindow.exitGame();
                break;
        }
    }
}
